

package jetbrains.buildServer.server.rest.model.user;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import jetbrains.buildServer.server.rest.model.Fields;
import jetbrains.buildServer.server.rest.swagger.annotations.ModelDescription;
import jetbrains.buildServer.server.rest.util.ValueWithDefault;
import org.jetbrains.annotations.NotNull;

/**
 * @author Yegor.Yarko
 * Date: 20/09/2017
 */
@SuppressWarnings("PublicField")
@XmlRootElement(name = "permission")
@XmlType(name = "permission")
@ModelDescription(
  value = "Represents a permission.",
  externalArticleLink = "https://www.jetbrains.com/help/teamcity/role-and-permission.html",
  externalArticleName = "Roles"
)
public class Permission {
  @XmlAttribute
  public String id;

  @XmlAttribute
  public String name;

  /**
   * True, if this permission could be assigned only globally.
   * False, if permission could be assigned to specific project.
   * This field does not mean if permission is assigned to project or globally.
   */
  @XmlAttribute
  public Boolean global;

  public Permission() {
  }

  public Permission(
    @NotNull jetbrains.buildServer.serverSide.auth.Permission permission,
    @NotNull final Fields fields
  ) {
    id = ValueWithDefault.decideDefault(fields.isIncluded("id"), () -> permission.name().toLowerCase());
    name = ValueWithDefault.decideDefault(fields.isIncluded("name"), () -> permission.getDescription());
    global = ValueWithDefault.decideIncludeByDefault(fields.isIncluded("global"), () -> !permission.isProjectAssociationSupported());
  }

  public Permission(String id) {
    this.id = id;
  }
}